﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QPainter>
#include <QApplication>

// custom headers
#include "EditingParametersTableHeaderView.h"
#include "EditingParametersTableSymbols.h"
#include "DebugMessage.h"


#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{
    EdittingParametersTableHeaderView::EdittingParametersTableHeaderView(Qt::Orientation orientation, QWidget *parent)
        : QHeaderView(orientation, parent)
    {

    }

    EdittingParametersTableHeaderView::~EdittingParametersTableHeaderView()
    {
    }

    void EdittingParametersTableHeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {
        // 水平表头
        if (orientation() == Qt::Horizontal)
        {
            // draw horizontal header cells
            QMargins cellMargin(8, 0, 8, 0);
            switch (logicalIndex)
            {
            case COL_PARAMETER_NAME: // 参数名
            case COL_PARAMETER_VALUE: // 参数值
            case COL_PARAMETER_UNIT: // 单位
            case COL_PARAMETER_INSTANCE_MARK: // 实例标记
            {
                painter->save();

                // draw background
                DBG_WARN_AND_RETURN_VOID_UNLESS(model(), L"model()为空", L"GMEP", L"2024-01-30");
                QBrush &background = model()->headerData(logicalIndex, orientation(), Qt::BackgroundRole).value<QBrush>();
                painter->fillRect(rect, background);

                // draw text
                QString text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).value<QString>();
                Qt::AlignmentFlag textAlignment = (Qt::AlignmentFlag)(model()->headerData(logicalIndex, orientation(), Qt::TextAlignmentRole).value<int>());
                QFont font = model()->headerData(logicalIndex, orientation(), Qt::FontRole).value<QFont>();
                QColor foreground = model()->headerData(logicalIndex, orientation(), Qt::ForegroundRole).value<QColor>();
                painter->setFont(font);
                painter->drawText(rect.marginsRemoved(cellMargin), text, QTextOption(textAlignment));

                painter->restore();
                break;
            }
            default:
                QHeaderView::paintSection(painter, rect, logicalIndex);
                break;
            }
        }
        else
        {
            QHeaderView::paintSection(painter, rect, logicalIndex);
        }
    }
}
